package com.xiaobao.coalproperty;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xiaobao.coalproperty.module.find.view.FindFragment;
import com.xiaobao.coalproperty.module.home.view.HomeFragment;
import com.xiaobao.coalproperty.module.project.view.ProjectFragment;
import com.xiaobao.coalproperty.module.user.view.UserFragment;
import com.xiaobao.coalproperty.other.adapter.TabAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_view_pager)
    ViewPager mViewPager;
    @BindView(R.id.main_radio_group)
    RadioGroup mRadioGroup;
    @BindView(R.id.main_home)
    RadioButton rbtHome;
    @BindView(R.id.main_project)
    RadioButton rbtProject;
    @BindView(R.id.main_find)
    RadioButton rbtFind;
    @BindView(R.id.main_user)
    RadioButton rbtUser;

    private List<Fragment> fragments;
    private RadioButton[] mRadioButtons;
    private String[] titleStrings;
    private TabAdapter mTabAdapter;

    private static final String TAG = "MainActivity";


    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        showLeft(false);
        setTitle("首页");
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ProjectFragment());
        fragments.add(new FindFragment());
        fragments.add(new UserFragment());

        mRadioButtons = new RadioButton[]{rbtHome, rbtProject, rbtFind, rbtUser};
        titleStrings = new String[]{"首页", "项目", "发现", "我的"};

        mTabAdapter = new TabAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mTabAdapter);
        mViewPager.setCurrentItem(0);
        mRadioGroup.setOnCheckedChangeListener(mOnCheckedChangeListener);
    }

    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.main_home:
                    setRadioBackgroudn(0);
                    break;
                case R.id.main_project:
                    setRadioBackgroudn(1);
                    break;
                case R.id.main_find:
                    setRadioBackgroudn(2);
                    break;
                case R.id.main_user:
                    setRadioBackgroudn(3);
                    break;

            }
        }
    };


    private void setRadioBackgroudn(int index) {
        mViewPager.setCurrentItem(index);
        setTitle(titleStrings[index]);
        for (int i = 0; i < mRadioButtons.length; i++) {
            mRadioButtons[i].setChecked(false);
            if (i == index) {
                mRadioButtons[i].setChecked(true);
            }
        }
    }
}
